﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace LibNiconico.NiconicoApi.Extentions
{
    /// <summary>
    /// HttpWebRequest拡張
    /// </summary>
    public static class HttpWebRequestExtention
    {
        /// <summary>
        /// 非同期にHttpWebResponseを取得
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest req)
        {
            return (HttpWebResponse)await Task.Factory.FromAsync<WebResponse>(req.BeginGetResponse, req.EndGetResponse, null).ConfigureAwait(false);
        }

        /// <summary>
        /// 非同期にRequestStreamを取得
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async static Task<Stream> GetRequestStreamAsync(this HttpWebRequest req)
        {
            return (Stream)await Task.Factory.FromAsync<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream, null).ConfigureAwait(false);
        }
    }
}
